import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.image.WritableImage;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.robot.Robot;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Demo1 extends Application {

    Robot robot = new Robot();
    PixelReader pixelReader;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Rectangle2D bounds = Screen.getPrimary().getBounds();
        ImageView imageView = new ImageView();
        StackPane root = new StackPane(imageView);

        //
        imageView.setOnMouseMoved(event -> {
            Color color = pixelReader.getColor((int) event.getX(), (int) event.getY());
            System.out.println("rgb(%s,%s,%s);".formatted(
                    doubleToInt(color.getRed() * 255),
                    doubleToInt(color.getGreen() * 255),
                    doubleToInt(color.getBlue() * 255)));
        });

        Scene scene = new Scene(root, bounds.getWidth(), bounds.getHeight());
        scene.setFill(Color.TRANSPARENT);

        primaryStage.setOnShown(event -> {
            WritableImage screenCapture = robot.getScreenCapture(null, bounds);
            pixelReader = screenCapture.getPixelReader();
            imageView.setImage(screenCapture);
        });
        primaryStage.setX(0);
        primaryStage.setY(0);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        primaryStage.setTitle("Color Get");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static int doubleToInt(double i) {
        return (int) i;
    }

}
